﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Windows.Forms;

namespace Smugmug.HashMaker
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            string userName = txtEmailAddress.Text;
            string password = txtPassword.Text;
            if(!String.IsNullOrEmpty(userName) && !String.IsNullOrEmpty(password))
            {
                try
                {
                    string url =
                        String.Format(
                            "https://api.smugmug.com/services/api/rest/1.2.2/?method=smugmug.login.withPassword&APIKey=Q52tcyelZCIKQCCma1R8NGyXD29RPpFW&EmailAddress={0}&Password={1}",
                            HttpUtility.UrlEncode(userName), HttpUtility.UrlEncode(password));
                    var request = WebRequest.Create(url) as HttpWebRequest;
                    if (request != null)
                    {
                        using (var response = request.GetResponse() as HttpWebResponse)
                        {
                            if (response != null)
                            {
                                var reader = new StreamReader(response.GetResponseStream());
                                txtOutput.Text = reader.ReadToEnd();
                            }
                        }
                    }
                }
                catch(Exception ex)
                {
                    txtOutput.Text = ex.ToString();
                    btnCopyError.Visible = true;
                }
            }
        }

        private void btnCopyError_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.SetText(txtOutput.Text);
            }
            catch(Exception ex)
            {
                MessageBox.Show("Unable to copy message to clipboard", "Clipboard", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }
    }
}
